package com.zyw.controller;

import java.io.File;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zyw.crawler.services.CrawlerForGpgs;
import com.zyw.crawler.services.RequestObj;


@Controller
public class CrawlerController extends BaseController{
	
	@RequestMapping("/crawler_info_services.do")
	public void crawler_info_services(String dicXxzrlx,String dicXxhyzl,String dicXxssdq,String dicXxzbqs,String xxfcbj,String gjz,String[] fileType){
		if(state.equals("wait")){
			RequestObj obj=new RequestObj(dicXxzrlx, dicXxhyzl, dicXxssdq, dicXxzbqs, xxfcbj,fileType);
			if(gjz!=null){
				obj.addGjz(gjz.split(","));
			}
			CrawlerForGpgs.startTask(obj);
		}
	}
	@RequestMapping("/crawler_info_services_reset.do")
	public void crawler_info_services_reset(){
		state="wait";
		CrawlerForGpgs.stop();
		responseInfo("<script>window.close()</script>");
	}
	@RequestMapping("/crawler_info_services_stop.do")
	public void crawler_info_services_stop(){
		if(state.equals("stop"))state="wait";
		else state="stop";
		CrawlerForGpgs.stop();
		responseInfo("<script>window.close()</script>");
	}
	@RequestMapping("/crawler_task_listener.do")
	public void crawler_task_listener(){
		//System.out.println("当前服务状态:"+state+"  ->Time:"+Time.getNowTime());
		if(state.equals("wait")){
			responseInfo("<font color='#00DB00'>任务线程闲置中,选择条件后,点击抓取,开始执行任务。 --Server Time:"+Time.getNowTime()+"</font>");
		}else if(state.equals("running")){
			n=(n>=colors.length-1)?0:n+1;//循环颜色
			responseInfo("<font color='"+colors[n]+"'>任务运行中,请稍等。 --Server Time:"+Time.getNowTime()+"</font>");
		}else if(state.equals("end")){
			responseInfo("<font color='red'>任务结束,请单击  <a href='getResult.do' >下载</a> 获取任务结果。 --Server Time:"+Time.getNowTime()+"</font>");
		}else if(state.equals("analysis")){
			responseInfo("<font color='red'>文件下载结束,正在整理结果。 --Server Time:"+Time.getNowTime()+"</font>");
		}else if(state.equals("dabao")){
			responseInfo("<font color='red'>正在打包结果文件,请稍等。 --Server Time:"+Time.getNowTime()+"</font>");
		}else if(state.equals("stop")){
			responseInfo("<font color='red'>任务中止。 --Server Time:"+Time.getNowTime()+"</font>");
		}else{
			responseInfo("任务异常,点击抓取重试任务,如果仍旧异常,电话联系我。 --Server Time:"+Time.getNowTime()+"</font>");
		}
	}
	public static int n=0;
	public static String resultFilePath="";
	public static String [] colors={"#be77ff","#ca8eff","#ffa6ff","#ffaad5","#ff9797","#96fed1","#84c1ff","#ceceff","red"};
	@RequestMapping("/getResult.do")
	public void getResult(){
		responseFile("result.zip",new File(CrawlerForGpgs.class.getResource("/").getPath()+"/"+CrawlerForGpgs.dabao_File+".zip"));

	}
	public static String state="wait";
}
